package api_day12;

import java.util.Random;

public class Loader extends Thread{
    @Override
    public void run() {
        synchronized (Picture.o) {
            int progress;
            Random random = new Random();
            System.out.println("准备加载图片");
            while (Picture.process_load < 100){
                progress = random.nextInt(20);
                Picture.process_load += progress;
                if (Picture.process_load < 100) System.out.println("正在加载，当前进度：" + Picture.process_load + "%");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (Picture.process_load > 100) {
                    Picture.process_load = 100;
                    System.out.println("加载完成");
                    Picture.load = true;
                }
            }
            Picture.o.notify();
            try {
                Picture.o.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            while (Picture.process_download < 100){
                if (Picture.show){
                    progress = random.nextInt(20);
                    Picture.process_download += progress;
                    if (Picture.process_download < 100) System.out.println("正在下载，当前进度：" + Picture.process_download + "%");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    if (Picture.process_download > 100) {
                        Picture.process_download = 100;
                        System.out.println("下载完成");
                    }
                }
            }
        }
    }
}